﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IQKS.AWP.BusinessAccessLayer;
using IQKS.AWP.BusinessObject;
using System.Data;

using System.Data.SqlClient;
using System.Web.Security;
using IQKS.AWP.BusinessAccessLayer;
using IQKS.AWP.BusinessObject;
using System.Globalization;
using System.IO;
public partial class Admin_UserMasterModify : System.Web.UI.Page
{
    DataTable tb = new DataTable();
    DataTable tb1 = new DataTable();
    DataRow dr;
    int tbcountryid = 1;
    int tblocid = 1;
    //public string DocumentPath = ConfigurationSettings.AppSettings["ProfilePic"];
    public string TempDocumentPath = System.Configuration.ConfigurationSettings.AppSettings["TempProfilePic"];
    public string DocumentPath = System.Configuration.ConfigurationSettings.AppSettings["ProfilePic"];
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {

            if (Request.QueryString.Count > 0) //add
            {
                if (Convert.ToString(Request.QueryString["userid"]) == "0")
                {
                    lblTitle.Text = "Add User Master";
                    
                    region.Visible = false;
                    country.Visible = false;
                    countryhead.Visible = false;

                    tbfees.Visible = false;
                    tbCompanyDetails.Visible = false;
                    tbPayment.Visible = false;
                    lblMessage.Text = "";
                    tbAssessorLocalFees.Visible = false;

                    LoadBindData();
                    ClearData();
                    chkPP.Visible = false;
                    reqFU.Visible = false;
                    //txtPassword.Attributes.Add("value", txtPassword.Text);
                    //txtreenterPassword.Attributes.Add("value", txtreenterPassword.Text);    
                }
                else //edit
                {                    
                    lblTitle.Text = "Update User Master";
                    LoadBindData();
                    GetData(Request.QueryString["userid"]);
                    chkPP.Visible = true;
                    reqFU.Visible = false; //Ankit Patel 05/01/2016
                    chkPP.Checked = false; //Ankit Patel 05/01/2016
                }
            } 
        }
    }

    private void GetData(string userid)
    {
        UserMasterBAL objUMBal = new UserMasterBAL();
        UserMasterBO objUMBO = new UserMasterBO();
        Guid UserId = Guid.Parse(userid);
        //objUMBO = objUMBal.GetUserMaster(userid);
        trpassword.Visible = false;
        trRenterPass.Visible = false;
        trSecretQuestion.Visible = false;
        trSecretAnswer.Visible = false;
        tbAssessorLocalFees.Visible = false;
        if (objUMBO != null)
        {
            DataSet dsUserDetails = new DataSet();
            dsUserDetails = objUMBal.GetUserDetails(UserId);           

            if (dsUserDetails != null)
            {
                if (dsUserDetails.Tables.Count > 0)
                {
                    if (dsUserDetails.Tables[0].Rows.Count > 0)
                    {
                        ddlRoles.SelectedIndex = ddlRoles.Items.IndexOf(ddlRoles.Items.FindByText(dsUserDetails.Tables[0].Rows[0][1].ToString()));
                        //ddlRoles_SelectedIndexChanged(null, null);
                        RolesForUpdate();
                        ddlRoles.Enabled = false;
                    }                    
                    if (dsUserDetails.Tables.Count > 1)
                    { 
                        if (dsUserDetails.Tables[1].Rows.Count > 0)
                        {
                            txtEmailId.Text = dsUserDetails.Tables[1].Rows[0]["LoweredEmail"].ToString();
                            txtEmailId.Enabled = false;
                            txtSecret.Text = dsUserDetails.Tables[1].Rows[0]["PasswordQuestion"].ToString();
                            txtAnswer.Text = dsUserDetails.Tables[1].Rows[0]["PasswordAnswer"].ToString();
                            //FirstName, MiddleName,LastName, Gender,DOB, CountryID, RegionID
                            txtFirstName.Text = dsUserDetails.Tables[1].Rows[0]["FirstName"].ToString();
                            txtMiddleName.Text = dsUserDetails.Tables[1].Rows[0]["MiddleName"].ToString();
                            txtLasteName.Text = dsUserDetails.Tables[1].Rows[0]["LastName"].ToString();
                            txtDOB.Text ="";

                            string strFileName = string.Empty;
                            strFileName = Convert.ToString(dsUserDetails.Tables[1].Rows[0]["ProfilePicturePath"]);
                            // strFileName = txtEmail.Text.Trim();
                            if (strFileName == "")
                            {
                                Image1.ImageUrl = "~/Images/Unknown2.jpg";
                            }
                            else
                            {
                                Image1.ImageUrl = DocumentPath + '/' + strFileName;
                            }

                            RadioButtonList1.Items.FindByValue(dsUserDetails.Tables[1].Rows[0]["Gender"].ToString()).Selected = true;
                            ddlRegion.SelectedIndex = ddlRegion.Items.IndexOf(ddlRegion.Items.FindByValue(dsUserDetails.Tables[1].Rows[0]["RegionID"].ToString()));
                            GetCountryFromRegionData(Convert.ToInt32(ddlRegion.SelectedValue));
                            ddlCountry.SelectedIndex = ddlCountry.Items.IndexOf(ddlCountry.Items.FindByValue(dsUserDetails.Tables[1].Rows[0]["CountryID"].ToString()));
                            if (dsUserDetails.Tables[1].Rows[0]["CountryHeadID"].ToString() != "")
                            {
                                if (ddlRegion.SelectedIndex > 0 && ddlCountry.SelectedIndex > 0)
                                {
                                    GetCountryHeadData();
                                    ddlCountryHead.SelectedIndex = ddlCountryHead.Items.IndexOf(ddlCountryHead.Items.FindByValue(dsUserDetails.Tables[1].Rows[0]["CountryHeadID"].ToString()));
                                }
                            }
                        }
                    }
                    if (dsUserDetails.Tables.Count > 2)
                    {
                        if (dsUserDetails.Tables[2].Rows.Count > 0)
                        {
                            gvResult.DataSource = dsUserDetails.Tables[2];
                            gvResult.DataBind();
                            gvResult.Visible = true;
                            companydetails.Visible = true;
                            ViewState["dt"] = dsUserDetails.Tables[2];
                        }
                        else
                        {
                            gvResult.DataSource = null;
                            gvResult.DataBind();
                            gvResult.Visible = false;

                            companydetails.Visible = false;
                        }
                    }
                    if (dsUserDetails.Tables.Count > 3)
                    {
                        if (dsUserDetails.Tables[3].Rows.Count > 0)
                        {
                            //GraduationID,QualificationSpecialization,University,QualificationStatusID
                            ddlGraduation.SelectedIndex = ddlGraduation.Items.IndexOf(ddlGraduation.Items.FindByValue(dsUserDetails.Tables[3].Rows[0]["GraduationID"].ToString()));
                            txtSpecialization.Text = dsUserDetails.Tables[3].Rows[0]["QualificationSpecialization"].ToString();
                            txtUniversity.Text = dsUserDetails.Tables[3].Rows[0]["University"].ToString();
                            ddlStatus.SelectedIndex = ddlStatus.Items.IndexOf(ddlStatus.Items.FindByValue(dsUserDetails.Tables[3].Rows[0]["QualificationStatusID"].ToString()));

                        }
                    }
                    if (dsUserDetails.Tables.Count > 4)
                    {
                        if (dsUserDetails.Tables[4].Rows.Count > 0)
                        {
                            //Fees,PaymentMode,BankName,BankCode,SWIFTCode,AccountNo
                            txtFees.Text = dsUserDetails.Tables[4].Rows[0]["Fees"].ToString();
                            txtBankName.Text = dsUserDetails.Tables[4].Rows[0]["BankName"].ToString();
                            txtBankcode.Text = dsUserDetails.Tables[4].Rows[0]["BankCode"].ToString();
                            txtSWIFTCode.Text = dsUserDetails.Tables[4].Rows[0]["SWIFTCode"].ToString();
                            txtAccountNumber.Text = dsUserDetails.Tables[4].Rows[0]["AccountNo"].ToString();
                            if (dsUserDetails.Tables[4].Rows[0]["PaymentMode"].ToString() != "")
                            {
                                ddlPaymentMode.SelectedIndex = ddlPaymentMode.Items.IndexOf(ddlPaymentMode.Items.FindByText(dsUserDetails.Tables[4].Rows[0]["PaymentMode"].ToString()));
                            }
                            else
                            {
                                ddlPaymentMode.SelectedIndex = ddlPaymentMode.Items.IndexOf(ddlPaymentMode.Items.FindByText("--PaymentMode--"));
                            }
                        }
                    }
                    if (dsUserDetails.Tables.Count > 5)
                    {
                        if (dsUserDetails.Tables[5].Rows.Count > 0)
                        {
                            /*CountryID,StateName,ContactAddress,ZipCode
		                       --,Substring(PhoneNo,1,CHARINDEX('-',PhoneNo)-1) as 'PhoneCode'
		                       ,PhoneNo
		                      --,Substring(MobileNo,1,CHARINDEX('-',MobileNo)-1) as 'MobileCode'
		                      ,MobileNo
		                      --,Substring(OtherPhoneNo,1,CHARINDEX('-',OtherPhoneNo)-1) as 'OtherPhoneNoCode'
		                      ,OtherPhoneNo*/
                            txtState.Text = dsUserDetails.Tables[5].Rows[0]["StateName"].ToString();
                            txtAddress.Text = dsUserDetails.Tables[5].Rows[0]["ContactAddress"].ToString();
                            txtZipCode.Text = dsUserDetails.Tables[5].Rows[0]["ZipCode"].ToString();
                            if (dsUserDetails.Tables[5].Rows[0]["CountryID"].ToString() != "")
                            {
                                ddlCountry1.SelectedIndex = ddlCountry1.Items.IndexOf(ddlCountry1.Items.FindByValue(dsUserDetails.Tables[5].Rows[0]["CountryID"].ToString()));
                            }
                            else
                            {
                                ddlCountry1.SelectedIndex = ddlCountry1.Items.IndexOf(ddlCountry1.Items.FindByText("-- Select Country --"));
                            }
                            string phone=dsUserDetails.Tables[5].Rows[0]["PhoneNo"].ToString();
                            string pCode = "";
                            if (phone != "")
                            {
                                if (phone.Contains('-'))
                                {
                                    pCode = phone.Substring(0, phone.IndexOf('-'));
                                }                                
                                if (pCode != "")
                                {
                                    ddlCountrycode1.SelectedIndex = ddlCountrycode1.Items.IndexOf(ddlCountrycode1.Items.FindByText(pCode));
                                   // txtphoneno.Text = phone.Substring(phone.IndexOf('-') + 1, (phone.Length - (phone.IndexOf('-') + 1)));
                                    string[] strphone = phone.Split('-');
                                    if (strphone.Length == 3)
                                    {
                                        txtstdcode1.Text = strphone[1];
                                        txtphoneno.Text = strphone[2];
                                    }
                                    if (strphone.Length == 2)
                                    {
                                        txtphoneno.Text = strphone[1];
                                    }
                                }
                                else
                                {
                                    ddlCountrycode1.SelectedIndex = ddlCountrycode1.Items.IndexOf(ddlCountrycode1.Items.FindByText("--- Select ---"));
                                    txtphoneno.Text = phone;
                                }
                            }
                            else
                            {
                                txtphoneno.Text = "";
                            }

                            string mphone = dsUserDetails.Tables[5].Rows[0]["MobileNo"].ToString();
                            string mpCode = "";
                            if (mphone != "")
                            {
                                if (mphone.Contains('-'))
                                {
                                    mpCode = mphone.Substring(0, mphone.IndexOf('-'));
                                }
                                if (mpCode != "")
                                {
                                    ddlCountrycode2.SelectedIndex = ddlCountrycode2.Items.IndexOf(ddlCountrycode2.Items.FindByText(mpCode));
                                    txtmobile.Text = mphone.Substring(mphone.IndexOf('-') + 1, (mphone.Length - (mphone.IndexOf('-') + 1)));
                                }
                                else
                                {
                                    ddlCountrycode2.SelectedIndex = ddlCountrycode2.Items.IndexOf(ddlCountrycode2.Items.FindByText("--- Select ---"));
                                    txtmobile.Text = mphone;
                                }

                            }
                            else
                            {
                                txtmobile.Text = "";
                            }


                            string ophone = dsUserDetails.Tables[5].Rows[0]["OtherPhoneNo"].ToString();
                            string opCode = "";
                            if (ophone != "")
                            {
                                if (ophone.Contains('-'))
                                {
                                    opCode = ophone.Substring(0, ophone.IndexOf('-'));
                                }
                                if (opCode != "")
                                {
                                    ddlCountrycode3.SelectedIndex = ddlCountrycode3.Items.IndexOf(ddlCountrycode3.Items.FindByText(opCode));
                                   // txtotherphoneno.Text = ophone.Substring(ophone.IndexOf('-') + 1, (ophone.Length - (ophone.IndexOf('-') + 1)));
                                    string[] strotherphone = ophone.Split('-');
                                    if (strotherphone.Length == 3)
                                    {
                                        txtstdcode3.Text = strotherphone[1];
                                        txtotherphoneno.Text = strotherphone[2];
                                    }
                                    if (strotherphone.Length == 2)
                                    {
                                        txtotherphoneno.Text = strotherphone[1];
                                    }
                                }
                                else
                                {
                                    ddlCountrycode3.SelectedIndex = ddlCountrycode3.Items.IndexOf(ddlCountrycode3.Items.FindByText("--- Select ---"));
                                    txtotherphoneno.Text = ophone;
                                }
                            }
                            else
                            {
                                txtotherphoneno.Text = "";
                            }
                        }
                    }
                    //brijal add for Assessor Location fees
                    if (dsUserDetails.Tables.Count > 6)
                    {
                        if (dsUserDetails.Tables[6].Rows.Count > 0)
                        {
                            gvAssessorLocationFees.DataSource = dsUserDetails.Tables[6];
                            gvAssessorLocationFees.DataBind();
                            gvAssessorLocationFees.Visible = true;
                            //companydetails.Visible = true;
                            ViewState["dt1"] = dsUserDetails.Tables[6];
                        }
                        else
                        {
                            gvAssessorLocationFees.DataSource = null;
                            gvAssessorLocationFees.DataBind();
                            gvAssessorLocationFees.Visible = false;

                            //companydetails.Visible = false;
                        }
                    }
                }
            }
            //txtLanguageName.Text = objLMBO.LanguageName;
            //chkIsactive.Checked = Convert.ToBoolean(objLMBO.IsActive);
        }
    }
 
    private void LoadBindData()
    {
        GetRegionData();
        GetCountryData();
        GetRole();

        ActivityMasterBAL objActMstBAL = new ActivityMasterBAL();
        DataSet objjActMstDs = new DataSet();


        objjActMstDs = objActMstBAL.GetCountryMasterActive();

        CommonUtility.BindDropDown(ref ddlCountry1, objjActMstDs, "CountryName", "CountryID");
        CommonUtility.AddSelectToDropDownAsEmptyValue(ref ddlCountry1);


        CommonUtility.BindDropDown(ref ddlhCountryCode, objjActMstDs, "CountryTelCode", "CountryID");
        CommonUtility.AddSelectToDropDownAsEmptyValue(ref ddlhCountryCode);

        //creating objDev
        DataView objDv = new DataView();

        var lselColumns = new[] { "CountryTelCode" };
        DataTable uniqueCols = objjActMstDs.Tables[0].DefaultView.ToTable(true, lselColumns);
        objDv = uniqueCols.DefaultView;
        objDv.Sort = "CountryTelCode ASC";
        
        CommonUtility.BindDropDown(ref ddlCountrycode1, objDv, "CountryTelCode", "CountryTelCode");
        CommonUtility.AddSelectToDropDownAsEmptyValue(ref ddlCountrycode1);

        CommonUtility.BindDropDown(ref ddlCountrycode2, objDv, "CountryTelCode", "CountryTelCode");
        CommonUtility.AddSelectToDropDownAsEmptyValue(ref ddlCountrycode2);

        CommonUtility.BindDropDown(ref ddlCountrycode3, objDv, "CountryTelCode", "CountryTelCode");
        CommonUtility.AddSelectToDropDownAsEmptyValue(ref ddlCountrycode3);


        objjActMstDs = objActMstBAL.GetActivityByActivityType("GRADUATION");
        CommonUtility.BindDropDown(ref ddlGraduation, objjActMstDs, "ActivityName", "ActivityID");
        CommonUtility.AddSelectToDropDownAsEmptyValue(ref ddlGraduation);

        objjActMstDs = objActMstBAL.GetActivityByActivityType("QUALIFICATIONSTATUS");
        CommonUtility.BindDropDown(ref ddlStatus, objjActMstDs, "ActivityName", "ActivityID");
        CommonUtility.AddSelectToDropDownAsEmptyValue(ref ddlStatus);
                
        ddlCurentYear.DataSource = Enumerable.Range(0, 50).ToList();
        ddlCurentYear.DataBind();

        //tbAssessorLocalFees.Visible = false;
        //txtLocation.DataSource = new

        objjActMstDs = new LocationFeesMasterBAL().GetLocationFeesMaster();
        //CommonUtility.BindDropDown(ref ddlLocation, objjActMstDs, "Location", "Fees");
        CommonUtility.BindDropDown(ref ddlLocation, objjActMstDs, "Location", "Location");
        CommonUtility.AddSelectToDropDownAsEmptyValue(ref ddlLocation);

    }
    private void GetRegionData()
    {
        ddlRegion.DataSource = GetRegionDataSet();
       ddlRegion.DataTextField = "RegionName";
       ddlRegion.DataValueField = "RegionID";       
       ddlRegion.DataBind();
       ddlRegion.Items.Insert(0, new ListItem("-- Select Region --","0"));
    }

    private void GetRole()
    {
        ddlRoles.DataSource = GetRoleDataSet();
        ddlRoles.DataTextField = "RoleTitle";
        ddlRoles.DataValueField = "RoleName";
        ddlRoles.DataBind();
        CommonUtility.AddSelectToDropDownAsEmptyValue(ref ddlRoles);
    }

    private DataSet GetRoleDataSet()
    {

        DataSet objDs = new DataSet();
        try
        {
            objDs = new RoleMasterBAL().GetAllRoleDetails();
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }
        return objDs;

    }    

    private DataSet GetRegionDataSet()
    {

        DataSet objDs=null;
        try
        {
            objDs = new RegionMasterBAL().GetRegionMaster();
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog();// throw new ApplicationException(exception.Message);
        }
        return objDs;

    }
    
    private void GetCountryData()
    {
        ddlCountry1.DataSource = GetCountryDataSet();
        ddlCountry1.DataTextField = "CountryName";
        ddlCountry1.DataValueField = "CountryID";
        ddlCountry1.DataBind();
        ddlCountry1.Items.Insert(0, new ListItem("-- Select Country --", "0"));
    }

    private DataSet GetCountryDataSet()
    {
        DataSet objDs=null;
        try
        {
            objDs = new CountryMasterBAL().GetCountryMaster();
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog();// throw new ApplicationException(exception.Message);
        }
        return objDs;
    }

    private void GetCountryFromRegionData(int index)
    {
        ddlCountry.DataSource = GetCountryFromRegionDataSet(index);
        ddlCountry.DataTextField = "CountryName";
        ddlCountry.DataValueField = "CountryID";
        ddlCountry.DataBind();
        ddlCountry.Items.Insert(0, new ListItem("-- Select Country --", "0"));
    }

    private DataSet GetCountryFromRegionDataSet(int regionid)
    {
        DataSet objDs=null;
        try
        {
            objDs = new  ActivityMasterBAL().GetCountryFromRegionMaster(regionid);
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog();// throw new ApplicationException(exception.Message);
        }
        return objDs;

    }

    private void GetCountryHeadData()
    {
        ddlCountryHead.DataSource = GetCountryHeadDataSet(Convert.ToInt32(ddlRegion.SelectedValue.ToString()), Convert.ToInt32(ddlCountry.SelectedValue.ToString()));
        ddlCountryHead.DataTextField = "FullName";
        ddlCountryHead.DataValueField = "UserId";
        ddlCountryHead.DataBind();
        ddlCountryHead.Items.Insert(0, new ListItem("-- Select Country Head --", "0"));
    }
    private DataSet GetCountryHeadDataSet(int regionid,int countryid)
    {
        DataSet objDs=null;
        try
        {
            objDs = new CountryHeadBAL().GetCountryHead("CH", regionid, countryid); //IQ-KS Admin  Country Head
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }
        return objDs;

    }

    protected void BtnUserSubmite_Click(object sender, EventArgs e)
    {

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {

            UserMasterBAL objUMBal = new UserMasterBAL();
            UserMasterBO objUMBO = new UserMasterBO();

            if (Request.QueryString["mod"] == "up")
            {
                objUMBO.SetMethod = "UPDATE";
            }
            else
            {
                objUMBO.SetMethod = "INSERT";
            }
            objUMBO.UserID = Request.QueryString["userid"].ToString();
            objUMBO.Secret = txtSecret.Text.Trim();
            objUMBO.Answer = txtAnswer.Text.Trim();
            objUMBO.Password = txtPassword.Text.Trim();

            objUMBO.RoleName = ddlRoles.SelectedItem.Value.Trim();

            objUMBO.FirstName = txtFirstName.Text.Trim();
            objUMBO.MiddleName = txtMiddleName.Text.Trim();
            objUMBO.LastName = txtLasteName.Text.Trim();
            if(FileUpload2.PostedFile !=null)
            { 
                if (FileUpload2.PostedFile.FileName != "")
                {
                    TempFileSave();
                    objUMBO.ProfilePicturePath = System.IO.Path.GetFileName(hdnUploadPic.Value);// FileUpload2.FileName.Trim();//writing for displaying profile pic
                }
            }

            string strPath = string.Empty;
            string strFileExt = string.Empty;
            string strFileName = string.Empty;
        
           // strFileExt = Path.GetExtension(FileUpload2.PostedFile.FileName);

           // strFileName = FileUpload2.FileName.Replace(Convert.ToString(FileUpload2.FileName), txtEmailId.Text.Trim());
           // strPath = MapPath(Convert.ToString(ConfigurationManager.AppSettings["TempProfilePic"]));
           //string strpic = strPath + strFileName + strFileExt;
           // strpic = FileUpload2.SaveAs(strpic);

            objUMBO.Gender = RadioButtonList1.SelectedItem.Value.Trim();
            objUMBO.DOB = DateTime.ParseExact("01/01/2000", "dd/MM/yyyy", CultureInfo.InvariantCulture);//Convert.ToDateTime(txtDOB.Text.Trim());
            
            ////// checking fees value if it is not entered then assign it 0.00 changes as on 3JUL2015
            if (txtFees.Text.Trim() != "")
            { objUMBO.Fees = txtFees.Text.Trim(); }
            else
            { objUMBO.Fees = "0.00"; }
            
            objUMBO.PaymentMode = ddlPaymentMode.SelectedItem.Text.Trim();
            objUMBO.BankName = txtBankName.Text.Trim();
            objUMBO.BankCode = txtBankcode.Text.Trim();
            objUMBO.SWIFTCode = txtSWIFTCode.Text.Trim();
            objUMBO.AccountNo = txtAccountNumber.Text.Trim();

            objUMBO.CountryID = Convert.ToInt32(ddlCountry1.SelectedItem.Value.Trim());
            objUMBO.StateName = txtState.Text.Trim();
            objUMBO.Address = txtAddress.Text.Trim();
            objUMBO.ZipCode = txtZipCode.Text.Trim();
            if (ddlCountrycode1.SelectedValue != "0")
            {
                objUMBO.WorkPhoneNo = ddlCountrycode1.SelectedItem.Text.Trim() + "-" + txtstdcode1.Text.Trim() + "-" + txtphoneno.Text.Trim();
            }
            else
            { objUMBO.WorkPhoneNo = txtphoneno.Text.Trim(); }
            if (ddlCountrycode2.SelectedValue != "0")
            {
                objUMBO.MobileNo = ddlCountrycode2.SelectedItem.Text.Trim() + "-" + txtmobile.Text.Trim();
            }
            else
            { objUMBO.MobileNo = txtmobile.Text.Trim(); }
            if (ddlCountrycode3.SelectedValue != "0")
            {
                objUMBO.OtherPhoneNo = ddlCountrycode3.SelectedItem.Text.Trim() + "-" + txtstdcode3.Text.Trim() + "-" + txtotherphoneno.Text.Trim();
            }
            else
            { objUMBO.OtherPhoneNo = txtotherphoneno.Text.Trim(); }

            objUMBO.EmailID = txtEmailId.Text.Trim();
            //objUMBO.CreatedBy = txt.Text.Trim();            

            objUMBO.Graduation = Convert.ToInt32(ddlGraduation.SelectedValue);
            objUMBO.Specialization = txtSpecialization.Text.Trim();
            objUMBO.University = txtUniversity.Text.Trim();
            objUMBO.Status = Convert.ToInt32(ddlStatus.SelectedItem.Value.Trim());

            if (ddlCountryHead.SelectedValue != "0" && ddlCountryHead.SelectedValue !="")
            {
                objUMBO.CountryHeadID = Guid.Parse(ddlCountryHead.SelectedValue);
            }

            objUMBO.CreatedBy = CommonUtility.GetLoggedOnUserId();
            if (ddlCountry.SelectedValue != "" && ddlCountry.SelectedValue !="0")
            {
                objUMBO.RCountryID = Convert.ToInt32(ddlCountry.SelectedValue);
            }
            else
            { objUMBO.RCountryID = 0; }
            if (ddlRegion.SelectedValue != "" && ddlRegion.SelectedValue != "0")
            {
                objUMBO.RegionID = Convert.ToInt32(ddlRegion.SelectedValue);
            }
            else
            { objUMBO.RegionID = 0; }

            DataTable dtcheck = (DataTable)ViewState["dt"];

            if((DataTable)ViewState["dt"] != null)
            {
                string strNew = CommonUtility.ConvertDatatableToXML(((DataTable)ViewState["dt"]));
                objUMBO.UserCompanyDetail = strNew.Replace("Table2", "Table1");
            }
            if ((DataTable)ViewState["dt1"] != null)
            {
                string strNew = CommonUtility.ConvertDatatableToXML(((DataTable)ViewState["dt1"]));
                objUMBO.AssessorLocationFeesDetail = strNew.Replace("Table6", "Table1");
            }
            objUMBO.IsActive = true;
            object strReturnValue = objUMBal.SetUserMaster(objUMBO);
            if (strReturnValue.ToString() == "SUCCESS")
            {
                ClearData();
                if (FileUpload2.PostedFile != null)
                {
                    UploadedFileMove();
                }
                lblMessage.Text = "Record has been save succesfully.";
                lblMessage.ForeColor = System.Drawing.Color.Green;

                Response.Redirect("~/Admin/UserMaster.aspx");

            }
            //else if (strReturnValue == "EXIST")
            //{
            //    //lblMessage.Text = "User already exists."; //EnrollResources.GetMessage(Message.InsertMessage); //                   
            //}
            else
            {
                lblMessage.Text = Convert.ToString(strReturnValue); //EnrollResources.GetMessage(Message.FailedMessage); // 
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
    }

    private void ClearData()
    {
        txtSecret.Text = "";
        txtAnswer.Text = "";
        txtPassword.Text = "";

        ddlRoles.SelectedIndex = 0;

        txtFirstName.Text = "";
        txtMiddleName.Text = "";
        txtLasteName.Text = "";
        //FileUpload2.
        RadioButtonList1.SelectedIndex = 0;
        txtDOB.Text = "";

        txtFees.Text = "";
        ddlPaymentMode.SelectedValue = "0";
        txtBankName.Text = "";
        txtBankcode.Text = "";
        txtSWIFTCode.Text = "";
        txtAccountNumber.Text = "";

        ddlCountry.SelectedValue = "0";
        txtState.Text = "";
        txtAddress.Text = "";
        txtZipCode.Text = "";
        txtphoneno.Text = "";
        txtmobile.Text = "";
        txtotherphoneno.Text = "";

        txtEmailId.Text = "";
        //objUMBO.CreatedBy = txt.Text = "";            

        //objUMBO.Graduation = txt.Text = "";
        txtSpecialization.Text = "";
        txtUniversity.Text = "";
        ddlStatus.SelectedIndex = 0;
        ddlRegion.SelectedValue = "0";
        ClearCompany();
    }

    protected void RolesForUpdate()
    {

        switch (ddlRoles.SelectedValue)
        {
            case "RD":
                region.Visible = true;
                ddlRegion.SelectedValue = "0";
                country.Visible = false;
                countryhead.Visible = false;
                tbfees.Visible = false;
                 tbCompanyDetails.Visible = false;
                tbPayment.Visible = false;
                trpassword.Visible = false;
                trRenterPass.Visible = false;
                trSecretQuestion.Visible = false;
                trSecretAnswer.Visible = false;
                break;
            case "CH":
                region.Visible = true;
                country.Visible = true;       
                //ddlCountry.SelectedValue = "0";
                countryhead.Visible = false;
                ddlCountry.Enabled = false;
                tbfees.Visible = false;
                tbCompanyDetails.Visible = false; //making other tables false
                tbPayment.Visible = false;
                 trpassword.Visible = false;
                trRenterPass.Visible = false;
                trSecretQuestion.Visible = false;
                trSecretAnswer.Visible = false;
                break;
            case "CM":
                region.Visible = true;
                country.Visible = true;
                countryhead.Visible = true;
                
                ddlCountry.Enabled = false;
                ddlCountryHead.Enabled = false;
                tbfees.Visible = false;
                tbCompanyDetails.Visible = false;
                tbPayment.Visible = false;
                 trpassword.Visible = false;
                trRenterPass.Visible = false;
                trSecretQuestion.Visible = false;
                trSecretAnswer.Visible = false;
                break;
            case "RARL":
            case "RAM":
            case "AD":
            case "IE":
                region.Visible = true;
                country.Visible = true;
                tbfees.Visible = false;
                companydetails.Visible = false;
                countryhead.Visible = false;
                
                ddlCountry.Enabled = false;
                 tbCompanyDetails.Visible = false;  //making visible tables
                tbPayment.Visible = true;

                tb.Columns.Add("CompanyID", typeof(int));
                tb.Columns.Add("CompanyName", typeof(string));
                tb.Columns.Add("RoleInCompany", typeof(string));
                tb.Columns.Add("YearsInRole", typeof(string));

                ViewState["dt"] = tb; // here the blank tb is assinging need to be corrected

                tbAssessorLocalFees.Visible = true;
                tb1.Columns.Add("LocID", typeof(int));
                tb1.Columns.Add("Location", typeof(string));
                tb1.Columns.Add("Fees", typeof(int));
                ViewState["dt1"] = tb1;

                trpassword.Visible = false;
                trRenterPass.Visible = false;
                trSecretQuestion.Visible = false;
                trSecretAnswer.Visible = false;
                if (ddlRoles.SelectedValue == "RARL")
                    tbAssessorLocalFees.Visible = true;
                else
                    tbAssessorLocalFees.Visible = false;
                break;
            case "IQKSMG":
                region.Visible = false;
                country.Visible = false;
                countryhead.Visible = false;
                tbPayment.Visible = true;
                tbfees.Visible = false;
                tbCompanyDetails.Visible = false;
                 trpassword.Visible = false;
                trRenterPass.Visible = false;
                trSecretQuestion.Visible = false;
                trSecretAnswer.Visible = false;
                break;
            default:
                region.Visible = false;
                country.Visible = false;
                countryhead.Visible = false;
                tbfees.Visible = false;
               
                 tbCompanyDetails.Visible = false;
                tbPayment.Visible = false;
                break;

        }
    }

    protected void ddlRoles_SelectedIndexChanged(object sender, EventArgs e)
    {

        switch (ddlRoles.SelectedValue)
        {
            case "RD":
                region.Visible = true;
                //ddlRegion.SelectedValue = "0";
                country.Visible = false;
                countryhead.Visible = false;
                tbfees.Visible = false;
                tbCompanyDetails.Visible = false;
                tbPayment.Visible = false;
                break;
            case "CH":
                region.Visible = true;
                country.Visible = true;
                //ddlCountry.Items.Clear();
                //ddlCountry.Items.Insert(0, new ListItem("-- Select Country --", "0"));
                //ddlRegion.SelectedValue = "0";
                //ddlCountry.SelectedValue = "0";
                countryhead.Visible = false;
                //ddlCountry.Enabled = false;
                tbfees.Visible = false;
                tbCompanyDetails.Visible = false; //making other tables false
                tbPayment.Visible = false;
                //showrowsformanager();
                break;
            case "CM":
                region.Visible = true;
                country.Visible = true;
                countryhead.Visible = true;
                //ddlCountry.Items.Clear();
                //ddlCountry.Items.Insert(0, new ListItem("-- Select Country --", "0"));
                //ddlCountryHead.Items.Clear();
                //ddlCountryHead.Items.Insert(0, new ListItem("-- Select Country Head --", "0"));
                //ddlRegion.SelectedValue = "0";
                ddlCountry.Enabled = false;
                ddlCountryHead.Enabled = false;
                tbfees.Visible = false;
                //showrowsformanager();
                break;
            case "RARL":                
            case "RAM": 
                region.Visible = true;
                country.Visible = true;
                tbfees.Visible = false;
                companydetails.Visible = false;
                countryhead.Visible = false;
                tbCompanyDetails.Visible = false;  //making visible tables
                tbPayment.Visible = true;
                
                // ddlCountry.Items.Clear();
                // ddlCountry.Items.Insert(0, new ListItem("-- Select Country --", "0"));
                //// ddlRegion.SelectedValue = "0";
                // ddlCountry.Enabled = false;

                tb.Columns.Add("CompanyID", typeof(int));
                tb.Columns.Add("CompanyName", typeof(string));
                tb.Columns.Add("RoleInCompany", typeof(string));
                tb.Columns.Add("YearsInRole", typeof(string));

                ViewState["dt"] = tb;

                tbAssessorLocalFees.Visible = true;
                tb1.Columns.Add("LocID", typeof(int));
                tb1.Columns.Add("Location", typeof(string));
                tb1.Columns.Add("Fees", typeof(int));

                ViewState["dt1"] = tb1;

                if (ddlRoles.SelectedValue == "RARL")
                    tbAssessorLocalFees.Visible = true;
                else
                    tbAssessorLocalFees.Visible = false;

                // showrowsformanager();
                break;
            case "AD":
            case "IE":
                region.Visible = false;
                country.Visible = false;
                tbfees.Visible = false;
                companydetails.Visible = false;
                countryhead.Visible = false;
                tbCompanyDetails.Visible = false;  //making visible tables
                tbPayment.Visible = true;
                // ddlCountry.Items.Clear();
                // ddlCountry.Items.Insert(0, new ListItem("-- Select Country --", "0"));
                ddlRegion.SelectedValue = "0";
                // ddlCountry.Enabled = false;
                ddlCountry.SelectedValue = "0";
                tb.Columns.Add("CompanyID", typeof(int));
                tb.Columns.Add("CompanyName", typeof(string));
                tb.Columns.Add("RoleInCompany", typeof(string));
                tb.Columns.Add("YearsInRole", typeof(string));

                ViewState["dt"] = tb;
                // showrowsformanager();
                break;
            case "IQKSMG":
                region.Visible = false;
                country.Visible = false;
                countryhead.Visible = false;
                tbPayment.Visible = true;
                tbfees.Visible = false;
                tbCompanyDetails.Visible = false;
                //tbfees.Visible = true;
                //trcompanylabel.Visible = false;
                //trcompanychrorderlabel.Visible = false;
                //trcompanyNamelabel.Visible = false;
                //trcompanyRolelabel.Visible = false;
                //trcompanyNoOfYearslabel.Visible = false;
                //trcompanyButtonlabel.Visible = false;
               // trcompanylabel.Visible = false;
               // hiderowsformanager();
               
                break;
            default:
                region.Visible = false;
                country.Visible = false;
                countryhead.Visible = false;
                tbfees.Visible = false;
                tbCompanyDetails.Visible = false;
                tbPayment.Visible = false;
               // showrowsformanager();
                break;

        }
    }

    private void hiderowsformanager()
    {
        trcompanylabel.Attributes.Add("style", "display:none");
        trcompanychrorderlabel.Attributes.Add("style", "display:none");
        trcompanyNamelabel.Attributes.Add("style", "display:none");
        trcompanyRolelabel.Attributes.Add("style", "display:none");
        trcompanyNoOfYearslabel.Attributes.Add("style", "display:none");
        trcompanyButtonlabel.Attributes.Add("style", "display:none");
        trcompanylabel.Attributes.Add("style", "display:none");

    }

    private void showrowsformanager()
    {
        trcompanylabel.Attributes.Add("style", "display:block");
        trcompanychrorderlabel.Attributes.Add("style", "display:block");
        trcompanyNamelabel.Attributes.Add("style", "display:block");
        trcompanyRolelabel.Attributes.Add("style", "display:block");
        trcompanyNoOfYearslabel.Attributes.Add("style", "display:block");
        trcompanyButtonlabel.Attributes.Add("style", "display:block");
        trcompanylabel.Attributes.Add("style", "display:block");

    }

    protected void ddlRegion_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlRegion.SelectedIndex != 0)
        {
            ddlRoles_SelectedIndexChanged(sender, null);
            ddlCountry.Enabled = true;
            GetCountryFromRegionData(Convert.ToInt32(ddlRegion.SelectedValue));
        }
        else
        {
            ddlCountry.Items.Clear();
            ddlCountry.Enabled = false;
            ddlCountry.Items.Insert(0, new ListItem("-- Select Country --", "0"));
        }
    }        

    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlCountry.SelectedIndex != 0)
        {
            if (ddlRoles.SelectedValue == "CM")
            {
                int index2 = ddlCountry.SelectedIndex;
                ddlRoles_SelectedIndexChanged(sender, null);
                GetCountryFromRegionData(Convert.ToInt32(ddlRegion.SelectedValue));
                ddlCountry.SelectedIndex = index2;
                ddlCountry.Enabled = true;
                ddlCountryHead.Enabled = true;
                GetCountryHeadData();
            }
            else if (ddlRoles.SelectedValue == "CH" || ddlRoles.SelectedValue == "RARL" || ddlRoles.SelectedValue == "RAM" || ddlRoles.SelectedValue == "AD" || ddlRoles.SelectedValue == "IE")
            {
                ddlCountryHead.Items.Clear();
                ddlCountryHead.Enabled = false;
                ddlCountryHead.Items.Insert(0, new ListItem("-- Select Country Head--", "0"));
            }
            else
            {
          
                ddlCountryHead.Items.Clear();
                ddlCountryHead.Enabled = false;
                ddlCountryHead.Items.Insert(0, new ListItem("-- Select Country Head--", "0"));
            }
        }
        else
        {
            ddlCountry.Items.Clear();
            ddlCountry.Enabled = false;
            ddlCountry.Items.Insert(0, new ListItem("-- Select Country --", "0"));           
        }
    }

    protected void ddlCountryHead_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (ViewState["dt"] != null)
        {
            tb =(DataTable) ViewState["dt"] ;
        }
        else
        {
            tb = ViewState["dt"] as DataTable;
        }

        dr = tb.NewRow();
        ViewState["countryid"] = tbcountryid++;
        dr["CompanyID"] = (int)ViewState["countryid"];
        dr["CompanyName"] = txtCompanyName.Text;
        dr["RoleInCompany"] = txtCurrentRole.Text;
        dr["YearsInRole"] = ddlCurentYear.SelectedItem.Text;
        tb.Rows.Add(dr);
       // companydetails.Visible = true;
        gvResult.DataSource = tb;
        gvResult.DataBind();
        txtCompanyName.Text = "";
        txtCurrentRole.Text = "";
        ddlCurentYear.SelectedIndex = 0;
        companydetails.Visible = true;
        gvResult.Visible = true;
        ViewState["dt"] = tb;
    }
   
    protected void gvResult_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        tb = ViewState["dt"] as DataTable;        
        tb.Rows[e.RowIndex].Delete();
        tb.AcceptChanges();
        gvResult.DataSource = tb;
        gvResult.DataBind();
    }
    protected void btnCompanyClear_Click(object sender, EventArgs e)
    {
        ClearCompany();
    }
    protected void ClearCompany()
    {
        txtCompanyName.Text = "";
        txtCurrentRole.Text = "";
        ddlCurentYear.SelectedValue = "0";
    }
    protected void btnClearAll_Click(object sender, EventArgs e)
    {
        ClearData();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserMaster.aspx");
    }
    //protected void ddlCountry1_SelectedIndexChanged(object sender, EventArgs e)
    //{



    //}
    protected void ddlCountry1_SelectedIndexChanged1(object sender, EventArgs e)
    {

        ddlhCountryCode.SelectedIndex = ddlhCountryCode.Items.IndexOf(ddlhCountryCode.Items.FindByValue(Convert.ToString(ddlCountry1.SelectedValue)));
        string temp = (string)ddlhCountryCode.Items[ddlhCountryCode.SelectedIndex].Text;
        ddlCountrycode1.SelectedIndex = ddlCountrycode1.Items.IndexOf(ddlCountrycode1.Items.FindByValue(temp));
        ddlCountrycode2.SelectedIndex = ddlCountrycode1.Items.IndexOf(ddlCountrycode1.Items.FindByValue(temp));
        ddlCountrycode3.SelectedIndex = ddlCountrycode1.Items.IndexOf(ddlCountrycode1.Items.FindByValue(temp));
    }

    private void TempFileSave()
    {
        string strPath = string.Empty;
        string strFileExt = string.Empty;
        string strFileName = string.Empty;

        strFileExt = System.IO.Path.GetExtension(FileUpload2.PostedFile.FileName);

        strFileName = FileUpload2.FileName.Replace(Convert.ToString(FileUpload2.FileName), txtEmailId.Text.Trim());
        strPath = MapPath(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["TempProfilePic"]));

        hdnUploadPic.Value = strPath + strFileName + strFileExt;
        //FileUpload2.SaveAs(hdnUploadPic.Value);
        CommonUtility.SaveFile(FileUpload2, strPath, strFileName + strFileExt);
    }

    private int UploadedFileMove()
    {
        int intStatus = 0;
        string strProfileFilePath;

        if (hdnUploadPic.Value != "")
        {
            strProfileFilePath = MapPath(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["ProfilePic"]) + System.IO.Path.GetFileName(hdnUploadPic.Value));

            if (File.Exists(Path.Combine(strProfileFilePath, hdnUploadPic.Value)))
            {
                File.Delete(strProfileFilePath);
                File.Move(hdnUploadPic.Value, strProfileFilePath);
            }
            else
            {
                File.Move(hdnUploadPic.Value, strProfileFilePath);
            }
        }
        return intStatus;
    }
    protected void chkPP_CheckedChanged(object sender, EventArgs e)
    {
        if (chkPP.Checked)
        {
            FileUpload2.Visible = true;
            reqFU.Enabled = true;
        }
        else
        {
            FileUpload2.Visible = false;
            reqFU.Enabled = false;
        }
    }

    protected void btnAddLocFees_Click(object sender, EventArgs e)
    {
        if (ViewState["dt1"] != null)
        {
            tb1 = (DataTable)ViewState["dt1"];
        }
        else
        {
            tb1 = ViewState["dt1"] as DataTable;
        }

        dr = tb1.NewRow();
        ViewState["locid"] = tblocid++;
        dr["LocID"] = (int)ViewState["locid"];
        dr["Location"] = ddlLocation.SelectedItem.Text;
        dr["Fees"] = Convert.ToInt16(txtAssessLocationFees.Text);
        tb1.Rows.Add(dr);
        // companydetails.Visible = true;
        gvAssessorLocationFees.DataSource = tb1;
        gvAssessorLocationFees.DataBind();
        ddlLocation.SelectedValue = "0";
        txtAssessLocationFees.Text = "";
        //ddlCurentYear.SelectedIndex = 0;
        //companydetails.Visible = true;
        //gvResult.Visible = true;
        gvAssessorLocationFees.Visible = true;
        ViewState["dt1"] = tb1;

    }
    

    protected void btnClearLocFees_Click(object sender, EventArgs e)
    {
        ddlLocation.SelectedValue = "0";
        txtAssessLocationFees.Text = "";
    }

    protected void gvAssessorLocationFees_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        tb1 = ViewState["dt1"] as DataTable;
        tb1.Rows[e.RowIndex].Delete();
        tb1.AcceptChanges();
        gvAssessorLocationFees.DataSource = tb1;
        gvAssessorLocationFees.DataBind();
    }

    protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataSet objjActMstDs = new LocationFeesMasterBAL().GetLocationFeesMaster(ddlLocation.SelectedValue);
        txtAssessLocationFees.Text = objjActMstDs.Tables[0].Rows[0]["Fees"].ToString();
    }
}